package cn.kgc.vue.service.impl;

import cn.kgc.vue.aop.AopLog;
import cn.kgc.vue.aop.Opration;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.entity.Student;
import cn.kgc.vue.form.CourseLsitForm;
import cn.kgc.vue.mapper.StudentMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Course;
import cn.kgc.vue.service.CourseService;
import cn.kgc.vue.mapper.CourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
* @author 
* @description 针对表【course】的数据库操作Service实现
* @createDate 2023-08-19 11:46:39
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Override
    @AopLog(opration = Opration.SELECT,model = "课程模块")
    public ResponseResult getlist(CourseLsitForm courseLsitForm) {
        ResponseResult responseResult = new ResponseResult();
        Page<Course> page = new Page<>(courseLsitForm.getPage(), courseLsitForm.getLimit());
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(courseLsitForm.getCourseName()),Course::getCourseName,courseLsitForm.getCourseName()).orderByDesc(Course::getId);
        Page<Course> coursePage = courseMapper.selectPage(page, wrapper);
        List<Course> records = coursePage.getRecords();
        long total = coursePage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("course",records);
        responseResult.success(ResponseStatus.SUCCESS,map);
        return responseResult;
    }
    //删除
    @Override
    @Transactional
    public ResponseResult delete(String id) {
        ResponseResult responseResult = new ResponseResult();
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getCourseId,id);
        List<Student> students = studentMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(students)){
            responseResult.fail(ResponseStatus.COURSE_USER);
            return responseResult;
        }
        courseMapper.deleteById(id);
        responseResult.success(ResponseStatus.DELETE_SUCCESS);
        return responseResult;
    }
}




